package com.gxsf.wechatbook.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gxsf.wechatbook.pojo.*;
import com.gxsf.wechatbook.repo.ReserListRepository;
import com.gxsf.wechatbook.repo.ReserUserInfoRepository;
import com.gxsf.wechatbook.repo.UserSmsListRepository;
import com.gxsf.wechatbook.util.DateUtil;
import com.gxsf.wechatbook.util.Error;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

/**
 * @author BeijiRonin
 * @version 1.0.0
 * @date 2021/1/21 19:35
 */
@Service
public class ReserService {
    @Autowired
    ReserUserInfoRepository reserUserInfoRepository;
    @Autowired
    ReserListRepository reserListRepository;
    @Autowired
    UserSmsListRepository userSmsListRepository;

    /**
     * 预约
     *
     * @param userStr      用户的json
     * @param libraryStr   图书馆的json
     * @param reserListStr 预约时间段的json
     * @return
     */
    public Object reser(String userStr, String libraryStr, String reserListStr) {
        JSONObject userJson = JSON.parseObject(userStr);
        JSONObject libraryJson = JSON.parseObject(libraryStr);
        JSONObject reserJson = JSON.parseObject(reserListStr);
        // parse info
        User user = JSON.toJavaObject(userJson, User.class);
        if (user.getScope() < 80) {
            return Error.error(404, "您的违规次数过多，当前积分：" + user.getScope() + ",不允许预约");
        }
        LibraryList libraryList = JSON.toJavaObject(libraryJson, LibraryList.class);
        ReserList reserList = JSON.toJavaObject(reserJson, ReserList.class);
        // star operation
        String openTime = reserList.getOpenTime();

        //开放时间按'-'号分割
        String[] split = openTime.split("-");
        boolean dateVer = DateUtil.dateVer(split[0], split[1]);
        if (dateVer) {
            long nowPeople = reserList.getNowPeople();
            long maxPeople = reserList.getMaxPeople();
            if (nowPeople >= maxPeople) {
                return Error.error(500, "人数已满");
            } else {
                //获取该用户当前是否正在使用
                ReserUserInfo nowReserUserInfo = reserUserInfoRepository.findFirstByUserIdAndStatusOrderById(user.getId(), 0);
                if (nowReserUserInfo == null) {
                    //保存预约信息
                    ReserUserInfo reserUserInfo = new ReserUserInfo();
                    reserUserInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
                    reserUserInfo.setUpdateTime(new Timestamp(System.currentTimeMillis()));
                    reserUserInfo.setUserId(user.getId());
                    reserUserInfo.setStatus(0);
                    reserUserInfo.setReserListId(reserList.getId());
                    reserUserInfo.setLibraryId(libraryList.getId());
                    ReserUserInfo save = reserUserInfoRepository.save(reserUserInfo);
                    //当前预约添加人数
                    reserList.setNowPeople(nowPeople + 1);
                    reserListRepository.save(reserList);
                    //向用户发送消息
                    UserSmsList userSmsList = new UserSmsList();
                    userSmsList.setCreateTime(new Timestamp(System.currentTimeMillis()));
                    userSmsList.setUserId(user.getId());
                    userSmsList.setContent(
                            String.format(
                                    "%s%s%s%s%s",
                                    "亲爱的",
                                    user.getName(),
                                    "，您在",
                                    libraryList.getLibraryName(),
                                    "申请的预约已通过，请尽快前往管区，避免预约时间结束"
                            )
                    );
                    userSmsList.setImg(libraryList.getImg());
                    userSmsList.setStatus(0);
                    userSmsListRepository.save(userSmsList);
                    return save;
                } else {
                    return Error.error(500, "不可同时预约多个管区");
                }
            }
        } else {
            return Error.error(500, "不在当前预约时间内");
        }
    }

}
